#!/bin/bash
# lab3 – 用户、组、权限与文件操作脚本
# ------------------------------------------------------------

function s1_generate_a_series_of_files() {
    for i in {1..100}; do
        echo "$i" > "/tmp/new_file${i}"
    done
}

function s2_list_4_files_in_long_format_about_account_and_group() {
    ls -l /etc/{passwd,shadow,group,gshadow} | sort -d
}

function s3_find_items_related_to_given_username() {
    grep -h "$1" /etc/{passwd,shadow} | sort -d
}

function s4_find_items_related_to_given_group_name() {
    grep -h "$1" /etc/{group,gshadow} | sort -d
}

function s5_create_groups_for_given_group_names() {
    groupadd -f rnd
    groupadd -f itoperation
}

function s6_create_users_and_set_their_group_as_required() {
    useradd -m -G rnd adam
    useradd -m -G rnd bob
    useradd -m -G itoperation calvin
    useradd -m -G itoperation david
    useradd -m -G rnd edward
}

function s7_change_users_shell() {
    usermod -s /usr/bin/sh adam
    usermod -s /usr/bin/sh edward
}

function s8_set_password_change_policy() {
    for u in adam bob calvin david edward; do
        passwd -n 10 -x 60 -w 10 "$u"
    done
}

function s9_set_password_for_five_users() {
    for u in adam bob calvin david edward; do
        passwd -d "$u"
        usermod -L "$u"
    done
}

function s10_lock_bobs_password() {
    usermod -L bob
}

function s11_include_adam_into_wheel_group() {
    usermod -aG wheel adam
}

function s12_change_files_permission() {
    for i in {1..33}; do
        f="/tmp/new_file$((i*3))"
        chmod 640 "$f"
        setfacl -b "$f" 2>/dev/null || true
    done
}
